﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace CXMLStudioIDE.DataAccessLayer
{
    public class PropertiesDAL : CXMLStudioDAL
    {
        public DataTable GetPropertiesByEntityVersion(int i_ContextID, int i_EntityTypeVersionID)
        {
            DataTable dt = new DataTable();
            System.Data.SqlClient.SqlConnection con = base.GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.GetPropertiesForEntity", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("int_ContextID", i_ContextID);
            da.SelectCommand.Parameters.AddWithValue("int_EntityTypeVersionsID", i_EntityTypeVersionID);
            try
            {
                con.Open();
                da.Fill(dt);
            }
            catch 
            {
                return new DataTable();
            }
            finally
            {
                da.Dispose();
                con.Close();
            }
            return dt;
        }
        public DataTable SavePropertyValue(int i_ContextID, int i_EntityTypeVersionsID, string s_PropertyName,
                                            string s_PropertyValue, string s_GroupName, string s_PropertyType)
        {
            DataTable dt = new DataTable();
            System.Data.SqlClient.SqlConnection con = base.GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.SaveEntityProperty", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("int_ContextID", i_ContextID);
            da.SelectCommand.Parameters.AddWithValue("int_EntityTypeVersionsID", i_EntityTypeVersionsID);
            da.SelectCommand.Parameters.AddWithValue("vch_PropertyName", s_PropertyName);
            da.SelectCommand.Parameters.AddWithValue("vch_PropertyValue", s_PropertyValue);
            da.SelectCommand.Parameters.AddWithValue("vch_GroupName", s_GroupName);
            da.SelectCommand.Parameters.AddWithValue("vch_PropertyType", s_PropertyType);
            try
            {
                con.Open();
                da.Fill(dt);
            }
            catch
            {
                return new DataTable();
            }
            finally
            {
                da.Dispose();
                con.Close();
            }
            return dt;
        }

        public DataTable SaveBinaryControl(int i_EntityTypeVersionsID, string s_LineageKey,byte[] bin_Property, int i_PropertyContextID, int i_ControlTypeID)
        {
            DataTable dt = new DataTable();
            System.Data.SqlClient.SqlConnection con = base.GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.SaveBinaryControl", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("int_EntityTypeVersionsID", i_EntityTypeVersionsID);
            da.SelectCommand.Parameters.AddWithValue("vch_LineageKey", s_LineageKey);
            SqlParameter param = new SqlParameter("bin_Control", SqlDbType.Image, bin_Property.Length);
            param.Value = bin_Property;
            da.SelectCommand.Parameters.Add(param);
            //da.SelectCommand.Parameters.AddWithValue("bin_Property", param);
            da.SelectCommand.Parameters.AddWithValue("int_PropertyContextID", i_PropertyContextID);
            da.SelectCommand.Parameters.AddWithValue("int_ControlTypeID", i_ControlTypeID);
            try
            {
                con.Open();
                da.Fill(dt);
            }
            catch(Exception sqlex)
            {
                return new DataTable();
            }
            finally
            {
                da.Dispose();
                con.Close();
            }
            return dt;
        }

        public byte[] GetBinaryControl(int i_EntityTypeVersionsID, string s_LineageKey, int i_PropertyContextID, int i_ControlTypeID)
        {
            byte[] bytes = null;
            DataTable dt = new DataTable();
            System.Data.SqlClient.SqlConnection con = base.GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.GetBinaryControl", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("int_EntityTypeVersionsID", i_EntityTypeVersionsID);
            da.SelectCommand.Parameters.AddWithValue("vch_LineageKey", s_LineageKey);
            da.SelectCommand.Parameters.AddWithValue("int_PropertyContextID", i_PropertyContextID);
            da.SelectCommand.Parameters.AddWithValue("int_ControlTypeID", i_ControlTypeID);
            try
            {
                con.Open();
                da.Fill(dt);
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow row = dt.Rows[0];
                    if (row != null)
                    {
                        bytes = (byte[])row["bin_Control"];
                    }
                }
            }
            catch (Exception sqlex)
            {
                return null;
            }
            finally
            {
                da.Dispose();
                con.Close();
            }
            return bytes;
        }
    }
}
